//
//  OrderListModel.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/6/10.
//

import UIKit
import HandyJSON

class OrderListModel: HandyJSON {

    var collectionFreightAmount : String = ""
    var collectionFreightSurplus : String = ""
    var createTime : String = ""
    var deliveryDetailAddress : String = ""
    var id : String = ""
    var expectedDeliveryTime : String = ""
    var expectedDeliveryTimePeriod : String = ""
    var statusCopywriting : String = ""
    var warehouseFreight : String = ""
    var freightAmount : String = ""
    var insureAmount : String = ""
    var goodsName : String = ""
    var goodsNote : String = ""
    var goodsSize : String = ""
    var mailSn : String = ""
    var orderSn : String = ""
    var receiverCity : String = ""
    var receiverDetailAddress : String = ""
    var orderStatus : String = ""
    var receiverName : String = ""
    var receiverPhone : String = ""
    var receiverPhoneCode : String = ""
    var receiverRegion : String = ""
    var senderCity : String = ""
    var senderDetailAddress : String = ""
    var senderName : String = ""
    var senderPhone : String = ""
    var senderPhoneCode : String = ""
    var senderRegion : String = ""
    var cwName : String = ""
    var cwPhone : String = ""
    var currentNetPointName : String = ""
    var receiveAddress : String = ""
    var targetNetPointName : String = ""
    var stationName : String = ""
    var note : String = ""
    var totalAmount : String = ""
    var userId : String = ""

    var standardTimerValue : String = ""
    var symbol : String = ""
    var printNum : String = ""
    var printTime : String = ""
    var totalCollectionFreight : String = ""
    var androidDownloadUrl : String = ""
    var iosDownloadUrl : String = ""
    var currentCompanyCode : String = ""
    var currentStaffCode : String = ""

    var abnormalStatus : Int = 0
    var abnormalType : Int = 0
    var deliveryWay : Int = 0
    var expectedDays : Int = 0
    var isCollection : Int = 0
    var isInsure : Int = 0
    var goodsWeight : Int = 0
    var payStatus : Int = 0
    var payType : Int = 0
    var orderType : Int = 0
    
    var fromCityId : Int = 0
    var fromRegionId : Int = 0
    var toRegionId : Int = 0
    var printStatus : Int = 0
    var toCityId : Int = 0
    var collectionPayStatus : Int = 0

    var insureFreight : Double = 0
    var goodsLength : Double = 0
    var goodsWidth : Double = 0
    var goodsHeight : Double = 0
    var payAmount : Double = 0

    var supportDelivery : Bool = false

    var goodsPic : Array = Array<Any>()
    var imageIconAry : Array = Array<Any>()
    var payImageIconAry : Array = Array<Any>()
    var imageIDAry : Array = Array<Any>()
    var imgDataDicAry : Array = Array<Any>()

    public required init(){}
    
    func getAllSignStr() -> Array<String> {
        var mytitleArray:Array = Array<String>()
        let tag = Int(self.orderStatus)
        if tag == 18 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_airport), at: 0)
        } else if tag == 17 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_stationt), at: 0)
        }
        if self.orderType == 1 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_worry), at: 0)
        }
        if self.isCollection == 1 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_collectionDelivery), at: 0)
        }
        if self.isInsure == 1 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_support), at: 0)
        }
        if self.abnormalType == 1 {
            mytitleArray.insert(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_retreat), at: 0)
        }
        return mytitleArray
    }
    
    func getOrderState() -> String {
        if self.orderStatus == Constants.order_Delete  {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_deleted)
        } else if self.orderStatus == Constants.order_cancel {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_canceled)
        } else  if self.orderStatus == Constants.order_waitReceive  {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitReceive)
        } else if self.orderStatus == Constants.order_waitColloct {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitCollect)
        } else  if self.orderStatus == Constants.order_waitSorting  {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_WaitSorting)
        } else if self.orderStatus == Constants.order_storage {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_Storageing)
        } else  if self.orderStatus == Constants.order_waitSend  {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_waitSender)
        } else if self.orderStatus == Constants.order_transit{
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_carriage)
        } else  if self.orderStatus == Constants.order_waitDelivery  {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_delivery)
        } else if self.orderStatus == Constants.order_signed {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_signed)
        } else if self.orderStatus == Constants.order_station {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_stationed)
        } else {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_airported)
        }
    }
    
    func getAllBtnStr() -> Array<String> {
        var mytitleArray = Array<String>.init()
        //订单状态：-1->已删除;0->已取消；1->待接单；3->待揽收；5-待分拣；8->仓储中；;10->待发件；13-运输中待接单 15 代派件 16已签收 17 已送车站 18 已送机场
        let userType:String = WLUserModel.getUserType()
        let roleTypeAry:Array = WLUserModel.getRoleType()
        if userType == "2" {
            if self.orderStatus == Constants.order_waitReceive {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_cancelOrder))
                
                if roleTypeAry.contains(where: { (obj) -> Bool in
                    if obj as! Int == 7 || obj as! Int == 4 {
                        mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_print))
                        return true
                    } else {
                        return false
                    }
                }) {
                    
                }
            } else if self.orderStatus == Constants.order_waitColloct {
                if roleTypeAry.contains(where: { (obj) -> Bool in
                    if obj as! Int == 7 || obj as! Int == 4 {
                        mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_print))
                        return true
                    } else {
                        mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_logistics_after))
                        return false
                    } 
                }) {
                }
            } else if self.orderStatus == Constants.order_cancel {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_delete))
            } else if self.orderStatus == Constants.order_waitSorting || self.orderStatus == Constants.order_storage ||
                        self.orderStatus == Constants.order_waitSend || self.orderStatus == Constants.order_transit ||
                        self.orderStatus == Constants.order_waitDelivery || self.orderStatus == Constants.order_signed ||
                        self.orderStatus == Constants.order_station || self.orderStatus == Constants.order_air {
                            mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_logistics_after))

            }
        } else {
            if self.orderStatus == Constants.order_waitColloct {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_collect))
                if roleTypeAry.contains(where: { (obj) -> Bool in
                    if obj as! Int == 3 {
                        mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_modification))
                        return true
                    } else {
                        return false
                    }
                }) {
                }
            } else if self.orderStatus == Constants.order_waitSorting {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_income))
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Sorting))
            } else if self.orderStatus == Constants.order_storage {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_leave))
            } else if self.orderStatus == Constants.order_waitDelivery {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_state_sign))
                if self.abnormalType != 1 {
                    mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_rejection))
                }
            }
        }
        return mytitleArray
    }
    
    func getDetailBtnStr() -> Array<String> {
        var mytitleArray = Array<String>.init()
        let userType:String = WLUserModel.getUserType()
        let roleTypeAry:Array = WLUserModel.getRoleType()
        if userType == "2" {
            if self.orderStatus == Constants.order_waitReceive {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_cancelOrder))
            } else if self.orderStatus == Constants.order_waitReceive {
                mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_operate_DeleteOrder))
            }
        } else {
            if self.orderStatus == Constants.order_waitSorting || self.orderStatus == Constants.order_waitColloct {
                if roleTypeAry.contains(where: { (obj) -> Bool in
                    if obj as! Int == 7 || obj as! Int == 4 {
                        mytitleArray.append(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Print_mailNo))
                        return true
                    } else {
                        return false
                    }
                }) {
                    
                }
            }
        }
        return mytitleArray
    }
    
    func getDeliveryWay() -> String {
        if self.deliveryWay == 0 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_visit)
        } else if self.deliveryWay == 1 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Station)
        } else if self.deliveryWay == 2 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_Airport)
        }
        return ""
    }
    
    func getPayWay() -> String {
        if self.payType == 0 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_paidShipper)
        } else if self.payType == 1 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_topay)
        } else if self.payType == 2 {
            return ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_deduction)
        }
        return ""
    }
    
    func getClientInfo(isReceive:Bool) -> AddressModel {
        let addressModel = AddressModel()
        if isReceive {
            addressModel.detailAddress = self.receiverDetailAddress
            addressModel.phone = self.receiverPhone
            addressModel.city = self.receiverCity
            addressModel.name = self.receiverName
            addressModel.phoneCode = self.receiverPhoneCode
            addressModel.region = self.receiverRegion
            addressModel.cityId = self.toCityId
            addressModel.regionId = self.toRegionId
        } else {
            addressModel.detailAddress = self.senderDetailAddress
            addressModel.phone = self.senderPhone
            addressModel.city = self.senderCity
            addressModel.name = self.senderName
            addressModel.phoneCode = self.senderPhoneCode
            addressModel.region = self.senderRegion
            addressModel.cityId = self.fromCityId
            addressModel.regionId = self.fromRegionId
        }
        return addressModel
    }
    
    func synchClientInfoModelInfo(clientInfoModel:AddressModel,isReceive:Bool) {
        if isReceive {
            self.receiverDetailAddress = clientInfoModel.detailAddress
            self.receiverCity = clientInfoModel.city
            self.receiverPhone = clientInfoModel.phone
            self.receiverName = clientInfoModel.name
            self.receiverPhoneCode = clientInfoModel.phoneCode
            self.receiverRegion = clientInfoModel.region
            self.toCityId = clientInfoModel.cityId
            self.toRegionId = clientInfoModel.regionId
        } else {
            self.senderDetailAddress = clientInfoModel.detailAddress
            self.senderCity = clientInfoModel.city
            self.senderPhone = clientInfoModel.phone
            self.senderName = clientInfoModel.name
            self.senderPhoneCode = clientInfoModel.phoneCode
            self.senderRegion = clientInfoModel.region
            self.fromRegionId = clientInfoModel.regionId
            self.fromCityId = clientInfoModel.cityId
        }
    }
    
    func getTotalFreight() -> String  {
        let calue = (Double(self.freightAmount) ?? 0) + self.insureFreight + (Double(self.warehouseFreight) ?? 0)
        return String(calue)
    }

}
